<template>
  <div class="wallpaper-selector">
    <h3>选择壁纸</h3>
    
    <div class="wallpaper-tabs">
      <el-tabs v-model="activeTab">
        <el-tab-pane label="系统壁纸" name="system">
          <div class="wallpaper-grid">
            <div 
              v-for="wallpaper in wallpaperStore.systemWallpapers" 
              :key="wallpaper"
              class="wallpaper-item"
              :class="{ 'active': wallpaper === wallpaperStore.currentWallpaper }"
              @click="selectWallpaper(wallpaper)"
            >
              <img :src="wallpaper" alt="壁纸" />
            </div>
          </div>
        </el-tab-pane>
        
        <el-tab-pane label="我的壁纸" name="user">
          <div v-if="wallpaperStore.userWallpapers.length > 0" class="wallpaper-grid">
            <div 
              v-for="wallpaper in wallpaperStore.userWallpapers" 
              :key="wallpaper"
              class="wallpaper-item"
              :class="{ 'active': wallpaper === wallpaperStore.currentWallpaper }"
            >
              <img :src="wallpaper" alt="壁纸" />
              <div class="wallpaper-actions">
                <el-button 
                  type="primary" 
                  size="small" 
                  circle 
                  @click.stop="selectWallpaper(wallpaper)"
                >
                  <el-icon><Check /></el-icon>
                </el-button>
                <el-button 
                  type="danger" 
                  size="small" 
                  circle 
                  @click.stop="removeWallpaper(wallpaper)"
                >
                  <el-icon><Delete /></el-icon>
                </el-button>
              </div>
            </div>
          </div>
          <div v-else class="empty-wallpapers">
            <p>您还没有添加自定义壁纸</p>
          </div>
          
          <div class="upload-section">
            <el-upload
              action="#"
              :auto-upload="false"
              :show-file-list="false"
              accept="image/jpeg,image/png,image/jpg,image/gif"
              :on-change="handleFileChange"
            >
              <el-button type="primary">上传壁纸</el-button>
              <template #tip>
                <div class="el-upload__tip">
                  支持 JPG, PNG, GIF 格式，建议尺寸 1920x1080 或更高
                </div>
              </template>
            </el-upload>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useWallpaperStore } from '@/stores/wallpaper'
import { ElMessage } from 'element-plus'
import { Check, Delete } from '@element-plus/icons-vue'

const wallpaperStore = useWallpaperStore()
const activeTab = ref('system')

// 选择壁纸
const selectWallpaper = async (wallpaper: string) => {
  try {
    await wallpaperStore.setWallpaper(wallpaper)
    ElMessage.success('壁纸设置成功')
  } catch (error) {
    console.error('设置壁纸失败', error)
    ElMessage.error('设置壁纸失败')
  }
}

// 移除壁纸
const removeWallpaper = async (wallpaper: string) => {
  try {
    await wallpaperStore.removeUserWallpaper(wallpaper)
    ElMessage.success('壁纸已删除')
  } catch (error) {
    console.error('删除壁纸失败', error)
    ElMessage.error('删除壁纸失败')
  }
}

// 处理文件选择
const handleFileChange = async (file: any) => {
  if (!file) return
  
  // 校验文件类型
  const validTypes = ['image/jpeg', 'image/png', 'image/jpg', 'image/gif']
  if (!validTypes.includes(file.raw.type)) {
    ElMessage.error('仅支持JPG、PNG和GIF格式的图片')
    return
  }
  
  // 校验文件大小 (5MB)
  const maxSize = 5 * 1024 * 1024
  if (file.raw.size > maxSize) {
    ElMessage.error('文件大小不能超过5MB')
    return
  }
  
  try {
    // 上传壁纸
    ElMessage.info('正在上传壁纸，请稍候...')
    await wallpaperStore.addUserWallpaper(file.raw)
    ElMessage.success('壁纸上传成功')
  } catch (error) {
    console.error('上传壁纸失败', error)
    ElMessage.error('上传壁纸失败')
  }
}
</script>

<style scoped>
.wallpaper-selector {
  padding: 20px;
}

.wallpaper-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  grid-gap: 15px;
  margin-top: 20px;
}

.wallpaper-item {
  position: relative;
  cursor: pointer;
  border-radius: 6px;
  overflow: hidden;
  height: 90px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  transition: all 0.3s;
}

.wallpaper-item:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
}

.wallpaper-item.active {
  box-shadow: 0 0 0 3px var(--el-color-primary);
}

.wallpaper-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.wallpaper-actions {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  opacity: 0;
  transition: opacity 0.3s;
}

.wallpaper-item:hover .wallpaper-actions {
  opacity: 1;
}

.empty-wallpapers {
  padding: 30px;
  text-align: center;
  color: #909399;
}

.upload-section {
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid #eee;
}
</style> 